<?php
/**
 * Created by PhpStorm.
 * User: xpz
 * Date: 2022/4/2
 * Time: 9:38
 */

namespace app\api\command\spider;
use app\api\command\spider\SpiderBase;
use think\console\Command;
use think\console\Input;
use think\console\input\Option;
use think\console\Output;
use think\Exception;
use think\config;
use think\Db;


class Sohu extends SpiderBase
{
    private $baseDomain;
    private $createHtmlRootDir;
    private $createMHtmlRootDir;
    protected function configure()
    {
        $this->setName('sohu')              		//定义命令的名字
        ->setDescription('sohu')      //定义命令的描述
        ->addArgument('method')                   //参数1
        ->addArgument('id');                    //文章id
    }

    //    php think sohu spider
    protected function execute(Input $input, Output $output)
    {
//        $this->baseDomain = "https://www.gdwxcn.com";
//        require EXTEND_PATH.'phpQuery/phpQuery.php';
//        https://www.gdwxcn.com/xiaoshuo/
        ini_set('max_execution_time', 600);//秒为单位，自己根据需要定义
        $method = $input->getArgument('method');
        $this->createHtmlRootDir = ROOT_PATH."public/";
        $this->createMHtmlRootDir = "/www/wwwroot/m.lishiketang.com/";
        if($method == 'spider'){
            $this->spiderListUrl();
        } else if($method == 'test'){
            $this->test();
//            $this->spiderListUrl();
        } else if($method == 'create_one'){
            $this->createOne();
        }

    }
    private function test(){
        $sql = "select * from xpz_guesses";
        $rs = Db::query($sql);
//        print_r($rs);exit;
        if($rs){
            $articleModel = Model('app\common\model\GuessType');
            $guessesModel = Model('app\common\model\Guesses');
            foreach($rs as $k=>$v){
                $data = $articleModel->where(['name'=>$v['category_name']])->find();
//                print_r($data['id']);exit;
//                print_r(['category_id'=>$data['id']]);exit;
                $guessesModel->where(['id'=>$v['id']])->update(['category_id'=>$data['id']]);
            }
        }
    }

    private function spiderListUrl() {
        $this->ticheck();
        $url = 'https://v2.sohu.com/public-api/feed?scene=CATEGORY&sceneId=1338&page=1&size=20&callback=back';
//        $url = "https://v2.sohu.com/integration-api/mix/region/15899?page=1&size=10&requestId=2002271029175320_1585037290558&suv=2002271029175320&adapter=pc&pvId=1585037281585eaS3gHE&callback=back";
        $this->_baseUrl($url);
    }

    private function _baseUrl($url) {
        if (empty($url)) {
            echo "url地址不能为空！";
            exit;
        }
        $info = $this->_get_url($url);
        $info = ltrim($info,"/**/back(");
        $info = rtrim($info,');');
//        print_r($info);exit;
//        print_r(json_decode($info,true));exit;
        $arr = json_decode($info, true);
//        print_r($arr);exit;
        if (!empty($arr)) {
            foreach ($arr as $k => $v) {
                if($k > 5){//每次只抓取5篇
                    continue;
                }
//                print_r($v);
                $id = $v['id'];
                $authorId = $v['authorId'];
                if(!empty($id) && !empty($authorId)){
                    $images = [];
                    $oldPic = '';
                    if(!empty($v['images'])){
                        $images = $v['images'];
                    }
                    if(!empty($images)){
                        if(count($images) >=2){
                            $oldPic = $images[1];
                        } else {
                            $oldPic = $images[0];
                        }
                        if(!empty($oldPic)){
                            $oldPic = $this->downImage($oldPic);
                        }

                    }

                    $data = [
                        'title' => $v['title'],
                        'sohu_id' => $v['id'],
                        'author_id' => $v['authorId'],
                        'author_name' => $v['authorName'],
                        'source_url' => $url = "https://www.sohu.com/a/".$id."_".$authorId,
                        'images' => $v['images'],
                        'old_pic' => $oldPic,
                    ];
//                    print_r($data);exit;
                    $this->_getArticle($data);
                }
            }
        }
    }


    private function _getArticle($data) {
//        echo $data['url'];exit;
        $info = $this->_get_url($data['source_url']);
//        print_r($info);exit;
//        $info = file_get_contents($url);
        $meta_tags = get_meta_tags($data['source_url']);
//        print_r($meta_tags);
//        print_r($info);
//        exit;
        $preg = '/<article class="article" id="mp-editor">([\s\S]*?)<\/article>/i';
        preg_match_all($preg, $info, $res);
//        print_r($res);exit;
        $content = $res[1][0];
        if (!empty($content)) {
            $content = $this->_myTrimSouhu($content);
            $contentArr = $this->extractP($content,$data['title'],$data['old_pic']);

            $data['content'] = $contentArr[0];
            $data['summary'] = $contentArr[1];
            $meta_tags = get_meta_tags($data['source_url']);
            $data['keywords'] = $meta_tags['keywords'];

//            print_r($data);exit;
            $this->_insertArticle($data);
        } else {
            echo $data['url']."内容为空！";
        }

    }
    /*
     * 下载图片
     */
    private function downImage($imgUrl){
        $filename = md5($imgUrl).".jpg";
        $bhPath = sohuPath()."/".date('Ymd',time())."/";
        $save_dir = "/www/wwwroot/www.lishiketang.com/public/uploads/".$bhPath;
        $this->_base_getImage($imgUrl,$save_dir,$filename);
        $imgurl = "/uploads/".$bhPath.$filename;
        return $imgurl;
    }

    /**
     * 过滤搜狐内容
     * @param $str
     * @return mixed
     */
    private function _myTrimSouhu($str){
        $search = array('<!-- 政务处理 -->',' <!-- 政务账号添加来源标示处理 -->','<!--                                          -->','<a href="//www.sohu.com/?strategyid=00001 " target="_blank" title="点击进入搜狐首页" id="backsohucom" style="white-space: nowrap;"><span class="backword"><i class="backsohu"></i>返回搜狐，查看更多</span></a>');
        $replace = array("", "","","");
        return str_replace($search, $replace, $str);
    }

    /**
     * 整理内容到p标签
     * @param $content
     * @param $title
     * @param string $image
     * @return string
     */
    private function extractP($content,$title,$image='') {

//        print_r($content);exit;
        $string = "";
        $summary = '';
        preg_match_all('/<p(.*?)>(.[\s\S]*?)<\/p>/', $content, $result);

        $contentArr = $result[2];

//        print_r($contentArr);exit;
        if (!empty($contentArr)) {
            array_shift($contentArr);//删除数组第一个
            array_pop($contentArr);//删除数组最后一个
//            $pCount = count($contentArr);
            foreach ($contentArr as $k => $v) {
//                if($k == 0 || $k==($pCount-1) || $k==($pCount-2)){
//                    continue;
//                }

                if (!strstr($v, '<img')) {
                    $gshString = $this->myTrim($v);
                    $string .="<p>" . $gshString . "</p>";
                    if($k > 0){
                        $summary .= $gshString;
                    }

                } else {//不要图片
//                    $string .="<p class=\"img\">" . $v . "</p>";
                }
                //图片放到第二段
                if($k == 1){
                    if(!empty($image)){
                        $string .="<p class=\"img\"><img src='" . $image . "' title='".$title."'/></p>";
                    }

                }
            }
        } else {
            $string = $content;
        }
//    echo $string;exit;
        return [$string,str_cut($summary,150)];
    }


    /**
     *
     * @param $row
     */
    private function _insertArticle($row) {
        $articleModel = Model('app\common\model\Sohu');
        $articleData = $articleModel->where(['sohu_id'=>$row['sohu_id']])->find();
        if(empty($articleData)){
            $row['images'] = json_encode($row['images']);
            $row['add_time'] = time();
            $id = $articleModel->insertGetId($row);
            //更新create_path字段
            $createPath = "/".sohuPath()."/".date("Ymd",time())."/".$id.".html";
            $articleModel->where(['id'=>$id])->update(['create_path'=>$createPath]);

            \think\Console::call('createsohohtml', ['createonehtml',$id]);
            \think\Console::call('createsohohtml', ['createonehtml_m',$id]);
//            $this->createOneHtml($id);
//            $this->createOneMHtml($id);
        } else {
            echo $row['title']."文章已经抓取过\r\n";
        }
    }

    private function createOne(){
        $this->ticheck();
        $articleModel = Model('app\common\model\Sohu');
        //生成M站并提交百度
        $articleMData = $articleModel->where(['source_type'=>1,'is_baidu_m'=>1])->order('id desc')->limit(1)->select();
//        $sql = "SELECT id FROM `xpz_sohu` WHERE id>21521 and source_type=1 order by id asc limit 10000";
//        $articleMData = Db::query($sql);
//        print_r($articleMData);exit;
        if(!empty($articleMData)){
            foreach($articleMData as $k=>$v){
                \think\Console::call('createsohohtml', ['createonehtml',$v['id']]);
//                $this->createOneMHtml($v['id']);
//                $this->createOneHtml($v['id']);
            }
        }
    }


    private function createOneMHtml($id){
        $articleModel = Model('app\common\model\Sohu');
        $articleData = $articleModel->where(['id'=>$id,'source_type'=>1])->order('id desc')->find();
//        echo $articleData['m_url'];exit;
        if(!empty($articleData)){
//            $createPath = sohuPath()."/".date("Ymd",time())."/";
//            print_r($articleData['create_path']);exit;
            $createPathArr = explode('/',$articleData['create_path']);
            $createPath = $createPathArr[1]."/".$createPathArr[2]."/";
//            print_r($createPathArr);exit;
            $url = $this->domain.'/index.php/index/sohu/m_details?id='.$articleData['id'];
            echo $url."\r\n";
            $content = file_get_contents($url);
            if($content != false){
                $fileDir = $this->createMHtmlRootDir.$createPath;
                if (!is_dir($fileDir)) {
                    mkdirs($fileDir);
                }
                $htmlName = $fileDir.$articleData['id'].".html";
                $result = file_put_contents($htmlName,$content);
                if($result != false){
                    //状态改完已生成 is_html = 2
                    $createPathHtml = "/".$createPath.$articleData['id'].".html";
                    $articleModel->where(['id'=>$articleData['id']])->update(['is_html'=>2,'html_time'=>time(),'create_path'=>$createPathHtml]);
                    echo "生成详情页html成功！文件路径：".$htmlName."时间：".date('Y-m-d H:i:s')."\r\n";
                    //需重新生成列表页生成列表页
//                        $this->createOneList($articleData['typeid']);
                    //如果有关联作者，需要重新生成作者页面
//                        if(!empty($articleData['figure_id'])){
////                            echo $articleData['figure_id']."\r\n";
//                            \think\Console::call('figurecreatehtml', ['one',$articleData['figure_id']]);
//                        }
                    //推送到百度
                    if($articleData['is_baidu_m'] == 1){
                        $url = $articleData['m_url'];
                        echo "百度推送开始\r\n";
                        echo "百度推送地址".$url."\r\n";
                        \think\Console::call('baiduts', ['tuisong_m',$url]);
                        $articleModel->where(['id'=>$articleData['id']])->update(['is_baidu_m'=>2]);
                        echo "百度推送结束\r\n";
                    }

                } else {
                    echo "写入详情页html失败！文件路径：".$htmlName."时间：".date('Y-m-d H:i:s')."\r\n";
                }
            } else {
                echo "读取详情页内容失败！".$url."时间：".date('Y-m-d H:i:s')."\r\n";
            }


        } else {
            $articleModel->where(['id'=>$articleData['id']])->update(['status'=>2]);
            echo "文章ID为".$articleData['id']."的内容有可能不存在！\r\n";
        }
    }


    private function createOneHtml($id){
        $articleModel = Model('app\common\model\Sohu');
        $articleData = $articleModel->where(['id'=>$id,'source_type'=>1])->order('id desc')->find();
        if(!empty($articleData)){
            $createPath = sohuPath()."/".date("Ymd",time())."/";
            $url = $this->domain.'/index.php/index/sohu/details?id='.$articleData['id'];

            $content = file_get_contents($url);
            if($content != false){
                $fileDir = $this->createHtmlRootDir.$createPath;
                if (!is_dir($fileDir)) {
                    mkdirs($fileDir);
                }
                $htmlName = $fileDir.$articleData['id'].".html";
                $result = file_put_contents($htmlName,$content);
                if($result != false){
                    //状态改完已生成 is_html = 2
                    $createPathHtml = "/".$createPath.$articleData['id'].".html";
                    $articleModel->where(['id'=>$articleData['id']])->update(['is_html'=>2,'html_time'=>time(),'create_path'=>$createPathHtml]);
                    echo "生成详情页html成功！文件路径：".$htmlName."时间：".date('Y-m-d H:i:s')."\r\n";
                    //需重新生成列表页生成列表页
//                        $this->createOneList($articleData['typeid']);
                    //如果有关联作者，需要重新生成作者页面
//                        if(!empty($articleData['figure_id'])){
////                            echo $articleData['figure_id']."\r\n";
//                            \think\Console::call('figurecreatehtml', ['one',$articleData['figure_id']]);
//                        }
                    //推送到百度
                    if($articleData['is_baidu'] == 1){
//                        $url = getDomain().$createPathHtml;
//                        echo "百度推送开始\r\n";
//                        echo "百度推送地址".$url."\r\n";
//                        \think\Console::call('baiduts', ['tuisong',$url]);
//                        $articleModel->where(['id'=>$articleData['id']])->update(['is_baidu'=>2]);
//                        echo "百度推送结束\r\n";
                    }

                } else {
                    echo "写入详情页html失败！文件路径：".$htmlName."时间：".date('Y-m-d H:i:s')."\r\n";
                }
            } else {
                echo "读取详情页内容失败！".$url."时间：".date('Y-m-d H:i:s')."\r\n";
            }


        } else {
            $articleModel->where(['id'=>$articleData['id']])->update(['status'=>2]);
            echo "文章ID为".$articleData['id']."的内容有可能不存在！\r\n";
        }
    }



    private function spider(){

    }
}